package com.example.product.RocketMq;

import com.alibaba.fastjson.JSON;
import com.example.product.service.impl.ProductServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "product-consumer",
        topic = "PRODUCT_TOPIC"
)
public class MQProductListener implements RocketMQListener<String> {
    @Autowired
    private ProductServiceImpl productService;
    // 监听到消息就会执行此方法
    @Override
    public void onMessage(String key) {
        log.info("监听到消息：key={}", JSON.toJSONString(key));
        productService.deleteProductCache(key);
    }
}